//
//  ST_BTUploadProgressView.swift
//  BTVideo
//
//  Created by DouLei on 2019/9/4.
//  Copyright © 2019 DouLei. All rights reserved.
//

import UIKit
import SnapKit
import HandyJSON
import SwifterSwift
import RealmSwift

class ST_BTUploadProgressView: UIView {

    static let shared = ST_BTUploadProgressView.init(frame: CGRect.zero)
 
    static let fwWidth = ScreenWidth - 30
    
    static let fwHeight = 64.cgFloat
    
    private override init(frame: CGRect) {
        super.init(frame: frame)
        self.cornerRadius = 6
        self.backgroundColor = Color.white
        
        self.addSubview(cover_image_view)
        cover_image_view.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(10)
            make.top.equalToSuperview().offset(10)
            make.bottom.equalToSuperview().offset(-10)
            make.width.equalTo(cover_image_view.snp.height)
        }
        
        self.addSubview(uploading_label)
        uploading_label.snp.makeConstraints { (make) in
            make.left.equalTo(cover_image_view.snp.right).offset(10)
            make.centerY.equalTo(cover_image_view).offset(-10)
        }
        
        
        self.addSubview(slider_root_view)
        slider_root_view.snp.makeConstraints { (make) in
            make.left.equalTo(cover_image_view.snp.right).offset(10)
            make.top.equalTo(uploading_label.snp.bottom).offset(5)
            make.right.equalToSuperview().offset(-20)
            make.height.equalTo(2)
        }
        
        slider_root_view.addSubview(slider_value_view)
        slider_value_view.snp.makeConstraints { (make) in
            make.left.top.bottom.equalTo(slider_root_view)
            make.width.equalTo(1)
        }
        
        
        
        
        self.addSubview(success_label)
        success_label.snp.makeConstraints { (make) in
            make.left.equalTo(cover_image_view.snp.right).offset(10)
            make.centerY.equalTo(cover_image_view)
        }
        
        self.addSubview(look_btn)
        look_btn.snp.makeConstraints { (make) in
            make.left.equalTo(success_label.snp.right).offset(5)
            make.centerY.equalTo(cover_image_view)
            make.width.equalTo(60)
            make.height.equalTo(24)
        }
        
        
        self.shadowColor = UIColor(red: 0.47, green: 0.47, blue: 0.47, alpha: 0.3)
        
        self.shadowOffset = CGSize(width: 0, height: 1)
        
        self.shadowRadius = 5
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    MARK: - properties
   


    lazy var cover_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = UIView.ContentMode.scaleAspectFill
        control.cornerRadius = 4
        return control
    }()
    
    lazy var uploading_label:UILabel = {
        let control = UILabel.init()
        control.text = "视频上传中…"
        control.textColor = Color.init(hexString: "#333333")
//        control.numberOfLines = 0
//        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.regular)
        return control
    }()
    
    
    lazy var slider_root_view:UIView = {
        
        let control = UIView()
        control.backgroundColor = Color.init(hex: 0xD8D8D8)
        control.cornerRadius = 1
        return control
    }()
    
    lazy var slider_value_view:UIView = {
        
        let control = UIView()
        control.backgroundColor = Color.init(hex: 0xFEC207)
        control.cornerRadius = 1
        return control
    }()
    
    
    lazy var success_label:UILabel = {
            let control = UILabel.init()
            control.text = "发布成功"
            control.textColor = Color.init(hexString: "#333333")
    //        control.numberOfLines = 0
    //        control.textAlignment = .center
            control.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.regular)
        
        control.isHidden = true
            return control
    }()
    
     lazy var look_btn:UIButton = {
            
            let control = UIButton.init(type: UIButton.ButtonType.custom)
            control.backgroundColor = Color.init(hex: 0xFEC207)
            control.cornerRadius = 24/2.0
    //            control.borderColor = Color.init(white: 0, alpha: 0.1)
    //            control.borderWidth = 1
            control.setTitleForAllStates("去看看")
            control.setTitleColorForAllStates(.white)
            control.titleLabel?.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.regular)
            control.addTarget(self, action: #selector(preview_handle), for: UIControl.Event.touchUpInside)
        
            control.isHidden = true
        
            return control
    }()

    

    
//    MARK: - Progress Handle
    func upload(progressPercent:CGFloat)  {
        
        main_thread_execute {
            
            self.isHidden = false
            
            
            let width = self.slider_root_view.width * progressPercent
            
            self.slider_value_view.snp.updateConstraints { (make) in
                make.width.equalTo(width)
            }
            
            
            print("打印 progressPercent-",progressPercent)
            
        }
        
        
        
    }
    
    func start_upload(coverImage:UIImage){
        
//        slider_value_view.snp.updateConstraints { (make) in
//            make.width.equalTo(1)
//        }
        
        main_thread_execute {
            
            self.uploading_label.isHidden = false
            
            self.slider_root_view.isHidden = false
            
            self.look_btn.isHidden = true
            
            self.success_label.isHidden = true
            
            self.isHidden = false
            
    //        let data = try! Data.init(contentsOf: URL.init(fileURLWithPath: task.coverImagePath))
            
            self.cover_image_view.image = coverImage
        }
        
        
    }
    
    var upload_finished_id:String?
    
    func end_upload_status(_ status:ST_FWPushTaskStatus,videoId:String){
        
        main_thread_execute {
         
            self.upload_finished_id = videoId
            
            if status == .success{
                
                self.uploading_label.isHidden = true
                
                self.slider_root_view.isHidden = true
                
                self.look_btn.isHidden = false
                
                self.success_label.isHidden = false
                
                main_thread_execute(afterDelay: 5) {
                    
                    self.isHidden = true
                }
            }
            if status == .failure{
                self.isHidden = true
            }
            
            self.slider_value_view.snp.updateConstraints { (make) in
                make.width.equalTo(1)
            }
            
        }
    }
//    MARK: - preview Handle
    @objc func preview_handle(){
        
        guard let user = ST_BTUserInfoManager.shared.current_account_info?.userinfo,let id = upload_finished_id else { return  }
        
        JVrequest(PH.videoParticularApi,parameters:["video_id":id], success: { (json) in
            
            if let model =  UserPreviewVideoModel.deserialize(from: json["data"].dictionaryObject){
                
                let vc = ST_BTVideoParticularViewController()
                        
                vc.model = model
                
                vc.user = user
                
                vc.modalPresentationStyle = .fullScreen
                
                self.parentViewController?.present(vc, animated: true, completion: nil)
                
            }
            
            
        }) { (code, err) in
            
            print("看一看视频失败:",err)
        }
        
        
        
    }
    
}








enum ST_FWPushTaskStatus: Int {
    case normal = 0
    case uploading = 1
    case success = 2
    case failure = -1
}


class ST_BTVideoPushTask: Object {
    
    @objc dynamic  var id = UUID().uuidString
    
    @objc dynamic  var accountId = ""
    
    @objc dynamic  var videoPath = ""
    
    @objc dynamic  var coverImagePath = ""
    
    @objc dynamic  var time = Date()
    
    @objc dynamic var status = ST_FWPushTaskStatus.normal.rawValue
    
    var statusEnum: ST_FWPushTaskStatus {
        get { return ST_FWPushTaskStatus(rawValue: status)! }
        set { status = newValue.rawValue }
    }
    
//    MARK: push param
    @objc dynamic  var comments = ""
    
    @objc dynamic  var videoRemoteURL = ""
    
    @objc dynamic  var coverImageRemoteURL = ""
    
    @objc dynamic  var videoDuration = 0
//    音乐ID
    @objc dynamic  var bgmId = ""
//    滤镜ID
    @objc dynamic  var filterId = ""
    
//    是否全屏播放 1 全屏  2 留黑边
    @objc dynamic  var screen_type = 1
}
